import 'dart:convert';

import 'package:flutter_network/dio_manager.dart';

import 'base_callback.dart';
import 'base_response_bean.dart';

/// @author: pengboboer
/// @createDate: 2021/11/24
class NetRequestUtils {
  static void request({dynamic future, BaseCallback? callback}) async {
    try {
      Response? response = await future;
      late Map<String, dynamic> res;
      res = json.decode(response.toString());
      print('res---------------${res}');
      BaseResponseBean? bean = BaseResponseBean.fromMap(res);
      print('bean----------------${bean}');
      if (bean == null) {
        callback?.onError("后台数据异常");
        return;
      }
      if (bean.code == 200) {
        callback?.onSuccess(res);
      } else {
        callback?.onError(bean.msg ?? "");
        callback?.onErrorCode?.call(bean.code, bean.msg);
        callback?.onErrorMap?.call(res);
      }
    } catch (e) {
      callback?.onError(e.toString());
    }
  }
}
